package cc.rengu.igas.mcps.core.service.trans.stagepay;

import cc.rengu.igas.mcps.common.constant.McpsTreeNodeConstant;
import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.service.base.InstalQuerySevice;
import cc.rengu.igas.mcps.facade.request.StageTryRequest;
import cc.rengu.igas.mcps.facade.response.StageTryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.BinInfo;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.BinInfoService;
import cc.rengu.oltp.service.realize.impl.BinInfoServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * Created by zhangbin on 2020/4/1
 * 分期试算
 */
public class StageTryService extends InstalQuerySevice {
    @Override
    protected Object bizBeforeCust() throws Exception {
        StageTryRequest stageTryRequest = new StageTryRequest();
        ConvertUtil.convertOutput(stageTryRequest);
        return stageTryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.INST_ID);
        StageTryRequest stageTryRequest = (StageTryRequest) request;
        BizResponse bizResponse = new BizResponse();
        /**/
        StageTryResponse stageTryResponse = new StageTryResponse();
        //获取卡BIN信息并判断是否支持此类卡的渠道交易
        /*获取卡号*/
        String cardNo = stageTryRequest.getPayAcctNo();
        /*获取卡bin信息*/
        BinInfoService binInfoService = new BinInfoServiceImpl();
        BinInfo binInfo = binInfoService.getCardBinInfo(cardNo);

        //联调测试暂时写死返回值
        stageTryResponse.setProdStageType("00");
        stageTryResponse.setProdStageCode("04");
        stageTryResponse.setMchntStageNum("03");
        stageTryResponse.setStageLimitAmt("1");
        stageTryResponse.setStageSumFee("3");
        stageTryResponse.setStageFeeType("0");
        stageTryResponse.setStageFixFee("1");
        stageTryResponse.setStageFeeRate("3%");
        stageTryResponse.setStageFee("3");
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setResult(stageTryResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;

//        //获取分期产品信息
//        ProdStageInfoMapper prodStageInfoMapper = new ProdStageInfoMapperImpl();
//        List<McpsProdStageInfo> mcpsProdStageInfoList = prodStageInfoMapper.selectProdStageInfoByPK(instId, "04", stageTryRequest.getProdStageCode(), Integer.parseInt(stageTryRequest.getMchntStageNum()));
//        /*初始化 分期费率*/
//        String prodStageFee = "";
//        if (mcpsProdStageInfoList == null || mcpsProdStageInfoList.isEmpty()) {
//            rglog.error("分期产品不存在,cardType:<{}>,issuerCode:<{}>", binInfo.getCardType(), binInfo.getIssuerCode());
//            throw new BizException(RespCodeEnum.POS_INVALID_PAN.getRespCode(), RespCodeEnum.POS_INVALID_PAN.getRespDesc());
//        } else {
//            /*获取费率*/
//            for (McpsProdStageInfo mcpsProdStageInfo : mcpsProdStageInfoList) {
//                /*当前日期 与费率生效日期 和 费率失效日期进行比较*/
//                if (DateUtil.compareDate(DateUtil.getCurrentDate(), mcpsProdStageInfo.getFeeEffectiveDate()) >= 0 && DateUtil.compareDate(DateUtil.getCurrentDate(), mcpsProdStageInfo.getFeeExpiryDate()) <= 0) {
//                    prodStageFee = mcpsProdStageInfo.getProdStageFee();
//                }
//            }
//        }
//        /* TODO 调用银数分期试算接口*/
//        bizResponse.setRspSysId(AppParamConstant.IGAS);
//        bizResponse.setResult(stageTryResponse);
//        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        StageTryResponse stageTryResponse = (StageTryResponse) bizResponse.getResult();
        stageTryResponse.setRespCode(bizResponse.getRespCode());
        stageTryResponse.setRespDesc(bizResponse.getRespDesc());
        ConvertUtil.convertInput(stageTryResponse);
    }
}
